package com.antu.first.web.frontend;

import com.alibaba.fastjson.JSON;
import com.antu.first.bean.User;
import com.antu.first.dto.UserAccessToken;
import com.antu.first.dto.UserOperationExecution;
import com.antu.first.service.UserService;
import com.antu.first.util.wechat.WechatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/12.
 */
@RequestMapping("/wechat")
@Controller
public class WechatLocalLogin {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    public String wechatLogin(String code, HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try {
            UserAccessToken token = WechatUtil.getUserAccessToken(code);
            if(token == null){
                modelMap.put("success", false);
                modelMap.put("errMsg", "无法获取用户的OpenId");
                return JSON.toJSONString(modelMap);
            }else{
                String openId=token.getOpenId();
                //设置用户的OpenId，根据OpenId查询用户信息
                User user=new User();
                user.setuOpenId(openId);
                try {
                    User result=userService.selectUserByOpenId(user);
                    //如果查询出的信息不为空，则把用户的个人信息放入Session，并返回给前端
                    if (result!=null){
                        HttpSession session = request.getSession();
                        session.setAttribute("user",result);
                        String sessionId=session.getId();
                        modelMap.put("sessionId",sessionId);
                        modelMap.put("success", true);
                        modelMap.put("user", result);
                    }else {//如果查询出的用户信息为空，则返回相应状态，微信端再请求为用户注册
                        modelMap.put("success", false);
                        modelMap.put("errMsg", "您还未注册，请先注册！");
                        modelMap.put("token", token);
                    }
                } catch (Exception e) {
                    new RuntimeException("WechatLocalLogin-wechatLogin:处理用户信息出错！");
                }
            }
        } catch (IOException e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "微信服务器异常，请稍后重试！");
        }
        return JSON.toJSONString(modelMap);
    }

    @RequestMapping(value = "/registerUser", method = RequestMethod.POST)
    @ResponseBody
    public String registerUser(User user){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try{
            UserOperationExecution execution = userService.addUser(user);
            if(execution.getState() != 0){
                modelMap.put("success", false);
                modelMap.put("errMsg", execution.getStateInfo());
                System.out.println(modelMap);
                return JSON.toJSONString(modelMap);
            }
            modelMap.put("success", true);
            modelMap.put("user", execution.getUser());
        }catch(Exception e){
            modelMap.put("success", false);
            modelMap.put("errMsg", "服务器错误，请稍后重试！");
        }
        System.out.println(modelMap);
        return JSON.toJSONString(modelMap);
    }

}
